// src/router/index.js
import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue'
import LoginView from '../views/LoginView.vue'
import RegisterView from '../views/RegisterView.vue'
// --- 新增: 导入个人中心视图 ---
import ProfileView from '../views/ProfileView.vue'
// --- ---
import PostListView from '../views/PostListView.vue' // 新增
import PostDetailView from '../views/PostDetailView.vue' // 新增
// import AdminPanelView from '../views/AdminPanelView.vue' // 以后添加
import CreatePostView from '../views/CreatePostView.vue'
import EditPostView from '../views/EditPostView.vue'
import CategoryListView from '../views/CategoryListView.vue'
import CategoryDetailView from '../views/CategoryDetailView.vue'
import AdminCategoryManagement from '../views/AdminCategoryManagement.vue'
import MyPostsView from '../views/MyPostsView.vue'
import MyRepliesView from '../views/MyRepliesView.vue'
import AnnouncementDetailView from '../views/AnnouncementDetailView.vue'
import MyFavoritesView from '../views/MyFavoritesView.vue'
import CreateAnnouncementView from '../views/CreateAnnouncementView.vue'
import UserDetailView from '../views/UserDetailView.vue'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'home',
      component: HomeView,
    },
    {
      path: '/login',
      name: 'login',
      component: LoginView,
      meta: { requiresAuth: false },
    },
    {
      path: '/register',
      name: 'register',
      component: RegisterView,
      meta: { requiresAuth: false },
    },
    // --- 新增: 个人中心路由 ---
    {
      path: '/profile',
      name: 'profile',
      component: ProfileView,
      // 标记为需要认证
      meta: { requiresAuth: true },
    },
    // --- 新增: 帖子路由 ---
    {
      path: '/posts', // 帖子列表页
      name: 'post-list',
      component: PostListView,
      meta: { requiresAuth: false }, // 假设帖子列表公开
    },
    {
      path: '/posts/:id', // 帖子详情页
      name: 'post-detail',
      component: PostDetailView,
      meta: { requiresAuth: false }, // 假设帖子详情公开
      props: true, // 将路由参数 `id` 作为 props 传递给组件
    },
    {
      path: '/posts/create',
      name: 'create-post',
      component: CreatePostView,
      meta: { requiresAuth: true }, // 需要登录才能访问
    },
    {
      path: '/posts/:id/edit', // 编辑帖子页
      name: 'edit-post',
      component: EditPostView,
      meta: { requiresAuth: true }, // 需要登录
      // props: true // 如果需要将 id 作为 props 传递给 EditPostView
    },
    {
      path: '/categories', // 板块列表页
      name: 'category-list',
      component: CategoryListView,
      meta: { requiresAuth: false }, // 公开访问
    },
    {
      path: '/categories/:id', // 板块详情页 (展示该板块下的帖子)
      name: 'category-detail',
      component: CategoryDetailView,
      meta: { requiresAuth: false }, // 公开访问
      props: true,
    },
    {
      path: '/admin/categories', // 管理员板块管理页
      name: 'admin-category-management',
      component: AdminCategoryManagement,
      meta: { requiresAuth: true, requiresPermission: 'CATEGORY_MANAGE' }, // 需要认证和特定权限
    },
    // --- 新增: 用户中心子页面路由 ---
    {
      path: '/profile/posts',
      name: 'my-posts',
      component: MyPostsView,
      meta: { requiresAuth: true },
    },
    {
      path: '/profile/replies',
      name: 'my-replies',
      component: MyRepliesView,
      meta: { requiresAuth: true },
    },
    {
      path: '/profile/favorites',
      name: 'my-favorites',
      component: MyFavoritesView,
      meta: { requiresAuth: true },
    },
    {
      path: '/announcements/:id', // 公告详情页
      name: 'announcement-detail',
      component: AnnouncementDetailView,
      meta: { requiresAuth: false }, // 公告详情页通常公开访问
      props: true,
    },
    // --- 新增: 公告路由 ---
    {
      path: '/announcements/create',
      name: 'create-announcement',
      component: CreateAnnouncementView,
      meta: { requiresAuth: true }, // 需要认证
      // 可以添加更具体的权限元信息，例如 meta: { requiresAuth: true, requiresUsername: 'lgt' }
    },
    // --- 新增: 用户详情页路由 ---
    {
      path: '/users/:id', // 用户详情页
      name: 'user-detail',
      component: UserDetailView,
      meta: { requiresAuth: false }, // 公开访问
      props: true, // 将路由参数 `id` 作为 props 传递给组件
    },
  ],
})

// --- (如果之前未添加) 添加路由守卫 ---
import { useAuthStore } from '@/stores/auth'
import { ElMessage } from 'element-plus'

router.beforeEach((to, from, next) => {
  const authStore = useAuthStore()

  // 1. 检查是否需要认证
  const requiresAuth = to.matched.some((record) => record.meta.requiresAuth !== false)

  if (requiresAuth && !authStore.isAuthenticated) {
    // 需要认证但未登录，重定向到登录页
    next({
      name: 'login',
      query: { redirect: to.fullPath }, // 保存尝试访问的路径
    })
  } else if (authStore.isAuthenticated && to.name === 'login') {
    // 已登录用户访问登录页，重定向到首页
    next({ name: 'home' })
  } else {
    // 2. 如果需要认证且已登录，或不需要认证，则检查权限 (如果定义了)
    const requiresPermission = to.matched.some((record) => record.meta.requiresPermission)
    if (requiresPermission) {
      const permission = to.matched.find((record) => record.meta.requiresPermission)?.meta
        .requiresPermission
      // 检查用户是否拥有该权限
      const hasPermission = authStore.currentUser?.roles?.some((role) =>
        role.permissions?.some((perm) => perm.name === permission),
      )
      if (!hasPermission) {
        // 权限不足，重定向到首页或 403 页面
        ElMessage.error('权限不足')
        next({ name: 'home' }) // 或 next({ name: 'forbidden' });
        return // 阻止导航
      }
    }
    // 其他情况，正常导航
    next()
  }
})
// --- ---

export default router
